- Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy path895. Maximum Frequency Stack.go
45 lines (39 loc) · 879 Bytes
/
895. Maximum Frequency Stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package leetcode
typeFreqStackstruct {
freqmap[int]int
groupmap[int][]int
maxfreqint
}
funcConstructor895() FreqStack {
hash:=make(map[int]int)
maxHash:=make(map[int][]int)
returnFreqStack{freq: hash, group: maxHash}
}
func (this*FreqStack) Push(xint) {
if_, ok:=this.freq[x]; ok {
this.freq[x]++
} else {
this.freq[x] =1
}
f:=this.freq[x]
iff>this.maxfreq {
this.maxfreq=f
}
this.group[f] =append(this.group[f], x)
}
func (this*FreqStack) Pop() int {
tmp:=this.group[this.maxfreq]
x:=tmp[len(tmp)-1]
this.group[this.maxfreq] =this.group[this.maxfreq][:len(this.group[this.maxfreq])-1]
this.freq[x]--
iflen(this.group[this.maxfreq]) ==0 {
this.maxfreq--
}
returnx
}
/**
* Your FreqStack object will be instantiated and called as such:
* obj := Constructor();
* obj.Push(x);
* param_2 := obj.Pop();
*/